﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

using VietStar.Library.Commom.Module;

public partial class controls_client_cartupdate : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Cart cart = (Cart)Session["cart"];
            BindCart(cart);
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Cart cart = (Cart)Session["cart"];
        foreach (RepeaterItem item in UpdateCart.Items)
        {
            CheckBox remove = (CheckBox)item.FindControl("remove");
            Label id = (Label)item.FindControl("id");
            TextBox quant = (TextBox)item.FindControl("quantity");
            int i = Convert.ToInt32(id.Text);
            int q = Convert.ToInt32(quant.Text);
            if (remove.Checked)
                cart.RemoveItem(i);
            cart.Update(i, q);
        }
        BindCart(cart);
    }

    private void BindCart(Cart cart)
    {
        List<CartItem> list = new List<CartItem>();
        foreach (CartItem item in cart.CartData.Values)
        {
            list.Add(item);
        }
        UpdateCart.DataSource = list;
        UpdateCart.DataBind();
        sum.Text = cart.TotalPrice + " vnđ";
    }
    protected void btnBuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/CartDetail.aspx");
    }
}
